<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Smsmodel extends CI_Model {

	protected $_CI;

	public static $searchField = array();
	public static $likeField = array();

	public function __construct(){
		parent::__construct();
		$this->_CI = & get_instance();
	}
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
	public function getSmsList($assist = array()){
		$this->db->from('sms');
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		if(!empty($assist['where_in'])){
			foreach ($assist['where_in'] as $k => $v) {
				if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
				else {
					$query = $this->db->get();
					return array(array(),0);
				}
			}
		}
		$this->db->order_by('sms_id','desc');
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}
/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
	public function createSms($info){
		$this->db->insert('sms',$info);
		return $this->db->insert_id();
	}

/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/

/*
| -------------------------------------------------------------------
|  Extern Functions
| -------------------------------------------------------------------
*/
    public function sendSms($phone,$content,$project = '') {
        $info = array();
		$info['username'] = SMS_SERVICE_NORMAL_CHANNEL_USERNAME;
		$info['Password'] = SMS_SERVICE_NORMAL_CHANNEL_PASSWORD;

		$info['Phones'] = $phone;
		$info['Content'] = $content;

        $this->load->library('rest');
		$res = $this->rest->post(SMS_SEND_SERVICE_URL,$info);
        $this->createSms(array(
            'project' => $project,
            'ip' => $this->input->ip_address(),
            'phone' => $phone,
            'content' => $content,
            'result' => json_encode($res,JSON_UNESCAPED_UNICODE),
            'sms_time_create' => NOW,
        ));
        return $res;
    }
}
